package com.lg.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.erp.entity.*;
import com.lg.erp.service.IMenuService;
import com.lg.erp.service.IRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ligen
 * @since 2021-04-12
 */
@RestController
public class RoleFunController {

    @Autowired
    private IRoleMenuService roleFunService;
    @Autowired
    private IMenuService functionsService;

    @PostMapping("/role-functions")
    public JsonResult addRoleFunctions(@RequestBody SRoleMenu roleFun){
        //通过id获取该function对象
        SMenu function = functionsService.getById(roleFun.getMenuId());
        //通过该pid获得所有pid为这个的functions数组，用来最后做判断看是否都已经点击了
        List<SMenu> allSonFunction = functionsService.list(new QueryWrapper<SMenu>()
                .lambda().eq(SMenu::getMenuPid, function.getMenuPid()));
        //查看当前roleFun是否在数据库中是否存在，依靠roleId和funId,
        SRoleMenu theOne = roleFunService.getOne(new QueryWrapper<SRoleMenu>()
                .lambda()
                .eq(SRoleMenu::getRoleId, roleFun.getRoleId())
                .eq(SRoleMenu::getMenuId, roleFun.getMenuId()));
        //如果存在则跳过该动作，不存在则添加当前数据
        if (theOne==null){
            roleFunService.save(roleFun);
        }
        //判断是否是1级目录，如果是则查询所有pid是当前对象的子级目录
        if (function.getMenuPid().equals("1"))
        {
            List<SMenu> list = functionsService.list(new QueryWrapper<SMenu>()
                    .lambda().eq(SMenu::getMenuPid,function.getId()));
            //遍历依次添加
            for (SMenu functions : list) {
                roleFun.setMenuId(functions.getId());
                //继续判断是否存在，存在则跳过
                theOne = roleFunService.getOne(new QueryWrapper<SRoleMenu>()
                        .lambda()
                        .eq(SRoleMenu::getRoleId, roleFun.getRoleId())
                        .eq(SRoleMenu::getMenuId, roleFun.getMenuId()));
                if (theOne!=null){
                    continue;
                }
                roleFunService.save(roleFun);
            }
            //非一级目录则只执行这里
        }else{
        //最后判断是否所有子级目录被录入了，如果有一个不存在则直接中断
            for (SMenu functions : allSonFunction) {
                theOne = roleFunService.getOne(new QueryWrapper<SRoleMenu>()
                    .lambda()
                    .eq(SRoleMenu::getRoleId, roleFun.getRoleId())
                    .eq(SRoleMenu::getMenuId, functions.getId()));
            if (theOne==null){
                return new JsonResult(1,"添加部分权限成功",null);
            }
              }
        //如果通过了，最后添加一级目录进数据库
        roleFun.setMenuId(function.getMenuPid());
        roleFunService.save(roleFun);
            }
        return new JsonResult(1,"添加所有权限成功",null);
    }
    @DeleteMapping("/role-functions/{roleId}/{functionsId}")
    public JsonResult RoleFunctions(@PathVariable String roleId,@PathVariable String functionsId){
        //通过id获取该function对象
        SMenu function = functionsService.getById(functionsId);
        roleFunService.remove(new QueryWrapper<SRoleMenu>()
                .lambda()
                .eq(SRoleMenu::getRoleId,roleId)
                .eq(SRoleMenu::getMenuId,functionsId));
        //判断是否是1级目录，如果是则查询所有pid是当前对象的子级目录
        if (function.getMenuPid().equals("1")){
            List<SMenu> list = functionsService.list(new QueryWrapper<SMenu>()
                    .lambda().eq(SMenu::getMenuPid,function.getId()));
            //遍历依次删除
            for (SMenu functions : list) {
                roleFunService.remove(new QueryWrapper<SRoleMenu>()
                        .lambda()
                        .eq(SRoleMenu::getRoleId,roleId)
                        .eq(SRoleMenu::getMenuId,functions.getId()));
            }
        }
        return new JsonResult(1,"删除权限成功",null);
    }
}
